#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 5e3 + 10;

int dep[N], fa[N], n;
int u[N], v[N], w[N], ans = 1e9;
veg G[N];
vec pot;
bool ban[N];

void dfs(int x, int p) {
	fa[x] = p; pot.eb(x);
	for(auto v : G[x]) if(!ban[v.fi] && v.fi ^ p) dep[v.fi] = dep[x] + v.se, dfs(v.fi, x);
}

pii solve(int x) {
	dep[x] = 0; dfs(x, 0); int pos = x; for(auto v : pot) if(dep[v] > dep[pos]) pos = v;
	pot.clear(); dep[x = pos] = 0; dfs(pos, 0); for(auto v : pot) if(dep[v] > dep[pos]) pos = v;
	pii res = { dep[pos], dep[pos] }; int tres = dep[pos];
	pot.clear(); while(pos ^ x) chkmin(res.fi, max(dep[pos], tres - dep[pos])), pos = fa[pos];
	return res;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in; rep(i, 1, n - 1) u[i] = in, v[i] = in, w[i] = in, G[u[i]].eb(v[i], w[i]), G[v[i]].eb(u[i], w[i]);
	rep(i, 1, n - 1) {
		pii w1, w2;
		ban[v[i]] = true; w1 = solve(u[i]); ban[v[i]] = false;
		ban[u[i]] = true; w2 = solve(v[i]); ban[u[i]] = false;
		chkmin(ans, max({ w1.fi + w2.fi + w[i], w1.se, w2.se }));
	} printf("%d\n", ans);
	return 0;
}
